package com.catmai.pssmanager.dao.impl;

import java.sql.SQLException;
import java.util.List;

import com.catmai.pssmanager.dao.GoodsDao;
import com.catmai.pssmanager.po.Goods;

public class GoodsDaoImpl extends BasicDaoImpl<Goods> implements GoodsDao{
	
	@Override
	public List<Goods> findAll() throws SQLException {
		String sql = "select g.goods_id,g.goods_name,g.lastChange_date,g.goods_number,s.storehouse_address,x.store_area FROM goods g,storehouse s,storearea x WHERE g.store_area_id=x.storearea_id and x.store_house_id=s.storehouse_id";
		return this.getList(connection, sql);
	}

	@Override
	public void saveObject(Goods goods) throws SQLException {
		String sql = "INSERT INTO goods VALUES(?,?,?,?,?)";
		Object [] args = {goods.getGoods_Id(),goods.getGoods_Name(),goods.getLastchange_Date(),goods.getGoods_Number(),goods.getStore_Area_Id()};
		this.update(connection, sql, args);
	}

	@Override
	public void deleteObject(Integer goodsId) throws SQLException {
		String sql = "delete from goods where goods_id=?";
		this.update(connection, sql, goodsId);
	}

	@Override
	public void updateObject(Goods goods) throws SQLException {
		String sql = "update Goods set goods_name = ? ,lastChange_date = ?,goods_number = ?,store_area_id = ? where Goods_id = ?";
		Object [] args = {goods.getGoods_Name(),goods.getLastchange_Date(),goods.getGoods_Number(),goods.getStore_Area_Id(),goods.getGoods_Id()};
		this.update(connection, sql, args);
	}

	@Override
	public Goods getGoodsById(Integer goodsId) throws SQLException {
		String sql = "select g.goods_id,g.goods_name,g.lastChange_date,g.goods_number,g.store_area_id from goods g where goods_id=?";
		return this.get(connection, sql, goodsId);
	}

	@Override
	public List<Goods> getGoodsByAreaId(Integer areaId) throws SQLException {
		String sql = "select g.goods_id,g.goods_name,g.lastChange_date,g.goods_number,g.store_area_id from goods g where store_area_id=?";
		return this.getList(connection, sql,areaId);
	}

}
